home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-09-11 | 8.1 KB | 254 lines | [TEXT/CWIE] |
- // IACommon.h
- // Common types, constants, classes and macros used by IA code.
-
- #pragma once
- #ifndef IACommon_h
- #define IACommon_h
-
- #pragma import on
-
- // Copyright: © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
-
- #ifndef IA_BEGIN_IMPORTS
- // the following are used when building shared libraries to define imports & exports
- // by default, they're defined with a no-op pragma
-
- //#define IA_BEGIN_IMPORTS mark includes
- //#define IA_END_IMPORTS mark includes
- //#define IA_BEGIN_EXPORTS mark includes
- //#define IA_END_EXPORTS mark includes
-
- #define IA_BEGIN_IMPORTS import on
- #define IA_END_IMPORTS import reset
- #define IA_BEGIN_EXPORTS export on
- #define IA_END_EXPORTS export off
-
- #endif
-
- // some inline members generate out-of-line definitions which cause
- // conflicts when building a shared library. the following macros
- // allow inlines to be turned off or on, so that library clients see
- // only the out-of-line definitions, while the library may use the inline.
- #ifdef IA_NO_INLINES
- // use the out-of-line definitions from the library
- #define IA_INLINE
- #define IA_INLINE_DEF();
- #define IA_INLINE_DEF_BODY(BODY);
- #else
- // let definitions inline
- #define IA_INLINE inline
- #define IA_INLINE_DEF() {}
- #define IA_INLINE_DEF_BODY(BODY) { BODY; }
- #endif
-
- // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
-
-
- //#pragma IA_BEGIN_IMPORTS
- #include <stdlib.h>
- #ifndef IA_NO_EXCEPTIONS
- #include <stdexcept>
- #endif
- #include <string.h>
- //#pragma IA_END_IMPORTS
-
- #pragma IA_BEGIN_EXPORTS
-
- //// ** Primitive types **
- /// These names are chosen to be short and to not conflict with others used on the Mac.
- typedef unsigned char byte;
- typedef byte Byte;
- typedef long int32;
- typedef unsigned long uint32;
- typedef unsigned short uint16;
- typedef uint16 UInt16;
-
- typedef void* (*IAAllocFptr)(size_t size); // ptr is a user defined struct
- typedef void (*IADeallocFptr)(void* object); // ptr is a user defined struct
-
- //// ** IA globals **
- extern uint32 IADiskBlockSize; // determines default size for most i/o
- extern IAAllocFptr IAAllocationFunc; // allocation function call back
- extern IADeallocFptr IADeAllocationFunc; // deallocation function call back
-
- //// ** IAMalloc -- all IA memory allocation is done through this **
-
- // IAMalloc & IAFree may be used in place of malloc() and free()
- void* IAMalloc(size_t size);
- void IAFree(void* object);
- // IAMallocSied & IAFreeSized may be used when the size is known at free time.
- // They may be faster and/or use less memory.
- void* IAMallocSized(size_t size);
- void IAFreeSized(void* object, size_t size);
-
- /// handy macros for memory allocation
-
- // IAMallocStruct & IAFreeStruct
- #define IAMallocStruct(TYPE) (TYPE*)IAMallocSized(sizeof(TYPE))
- #define IAFreeStruct(MEM,TYPE) IAFreeSized(MEM, sizeof(TYPE))
-
- // IAMallocArray & IAFreeArray
- // Caution: this should not be used for classes with virtual member functions (e.g. IAObjects)
- // Caution: nor should it be used for classes whose default ctor or dtor do anything
- #define IAMallocArray(TYPE,SIZE) (TYPE*)IAMalloc(sizeof(TYPE) * (SIZE))
- // we distinguish array deletion to enable substitution of 'delete[]' rather than 'delete'
- #define IAFreeArray IAFree
- // variants for sized allocation
- #define IAMallocArraySized(TYPE,SIZE) (TYPE*)IAMallocSized(sizeof(TYPE) * (SIZE))
- #define IAFreeArraySized(MEM,TYPE,SIZE) IAFreeSized(MEM, sizeof(TYPE) * (SIZE))
-
- // in the debug libraries, prints some info about memory usage to the standard output
- void IAReportMemoryUsage();
-
- //// ** IAStruct: a base for IA structs -- allocated by IAMalloc **
-
- struct IAStruct {
- public:
- void* operator new(size_t size);// { return IAMalloc(size); }
- void operator delete(void* object);// { IAFree(object); }
- };
-
-
- //// ** IAObject: a base class for Information Access objects **
-
- class IAObject : public IAStruct {
- public:
- IAObject() {}
- // Ensure objects have a virtual destructor. This enables IADeleteOnUnwind et. al.
- IA_INLINE virtual ~IAObject() IA_INLINE_DEF()
- void* operator new(size_t size) { return IAMallocSized(size); }
- IA_INLINE void operator delete(void* obj, size_t size) IA_INLINE_DEF_BODY(IAFreeSized(obj, size))
- private:
- // Copy constructors cause no end of confusion in C++, so we don't use them.
- // Instead, in any class with a destructor, we:
- // . specify but don't define one (to generate link errors if it is ever called)
- // . make it private so that it can't be specialized.
- IAObject(IAObject&);
- };
-
- // enable stack-based deletion of pointers to IAObjects
- // this ensures that the destructor is called even when the stack is unwound on exception
- // the typical usage is something like:
- // { Foo* foo = FunctionReturningFooStar();
- // IADeleteOnUnwind delFoo(foo);
- // < code using foo> }
- class IADeleteOnUnwind {
- public:
- IADeleteOnUnwind(IAObject* o) : object(o) {}
- ~IADeleteOnUnwind(); // delete object;
- IAObject* object;
- private:
- void* operator new(size_t size); // stack allocate only
- };
-
- // permit stack-allocated variable-sized arrays
- // the typical usage is something like:
- // { Foo* foos = IAMallocArray(<variable>, <size>);
- // IADeleteArrayOnUnwind delFoos(foos);
- // < code using foos> }
- class IADeleteArrayOnUnwind {
- public:
- IADeleteArrayOnUnwind(void* a); // : array(a) {}
- ~IADeleteArrayOnUnwind(); // { IAFreeArray(array); }
- void* array;
- private:
- void* operator new(size_t size); // stack allocate only
- };
-
- // permit stack-allocated variable-sized arrays of pointers to IAObjects
- // the typical usage is something like:
- // { Foo** foos = IAMallocArraySized(Foo*, <size>);
- // < initialize foos >
- // IADeletePointerArrayOnUnwind delFoos(foos, <size>);
- // < code using foos> }
- class IADeletePointerArrayOnUnwind {
- public:
- IADeletePointerArrayOnUnwind(IAObject* a[], uint32 l);
- ~IADeletePointerArrayOnUnwind();
- IAObject** array;
- uint32 length;
- private:
- void* operator new(size_t size); // stack allocate only
- };
-
-
- //// ** IA Exceptions **
-
- // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
- #ifndef IA_NO_EXCEPTIONS
-
- class IAException : public exception
- {
- public:
- IAException( const char* message);
- virtual ~IAException();
-
- // virtual void Throw() const;
- virtual const char* What() const;
- virtual const char* GetLocation() const;
- virtual int32 GetCode() const;
-
- void SetLocation (const char* location);
- void SetCode (int32 code);
-
- private:
-
- virtual const char* what() const;
-
- char fMessage[250];
- char fLocation[50];
- int32 fCode;
- };
-
-
- #else
- // until compilers have better exception support, just throw integers
- typedef const int32 IAException;
- #endif
-
- typedef const int32 IAExceptionCode;
-
- // don't inline calls to throw() -- makes code smaller & faster!
- void IAThrowException(IAException exception); // { throw(exception); }
-
- //
-
- void _IAAssertionFunc(const char*, int, const char*, const char*, IAExceptionCode code);
- #define IAAssertion(expression, message, code) \
- ( (expression) ? (void) 0 : (_IAAssertionFunc(__FILE__, __LINE__, #expression, message, code)) )
- //
-
- // define IAThrow to shadow definitions in this file
- #ifndef IAThrow
-
-
- // define IA_NO_EXCEPTIONS if your compiler does not yet support exceptions
- #ifndef IA_NO_EXCEPTIONS
- // exceptions are ANSI by default
- #define IAThrow(EXCEPTION) IAThrowException(EXCEPTION) // throw(exception)
- #define IATry try
- #define IACatch(BINDING) catch (BINDING)
-
- #else
-
- // definitions for compilers that don't support exceptions
- #define IAThrow(EXCEPTION) IAThrowException(EXCEPTION) // exit(EXCEPTION)
- #define IATry if (true)
- #define IACatch(BINDING) for (BINDING = 0; false; )
-
- #endif
- #endif
-
- IAExceptionCode IAAssertionFailure = 'VTWN';
-
- // handy macros which conditionally throw exceptions
- #define IAAssert(VALUE) if (!(VALUE)) (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAAssert", IAAssertionFailure))
- #define IAThrowIf(VALUE, EXCEPTION) if ( VALUE ) (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAThrowIf", EXCEPTION))
- #define IAThrowIfNot(VALUE, EXCEPTION) if (!(VALUE)) (_IAAssertionFunc(__FILE__, __LINE__, #VALUE, "IAThrowIfNot", EXCEPTION))
-
- #pragma IA_END_EXPORTS
-
- #pragma import reset
-
- #endif
-